package com.rgs.se.design.factory.simple;



import com.rgs.se.design.factory.Person;
import com.rgs.se.design.factory.Student;
import com.rgs.se.design.factory.Teacher;

import java.util.Objects;

/**
 * @author Ren Gaoshuai
 * @ClassName PersonFactory
 * @Description 简单工厂模式
 * @date 2022-02-28 15:46
 **/
public class PersonFactory<T> {

    /*begin********简单工厂模式*******************/
    /**
     * 反射创建对象
     * @param clazz 类对象
     * @param <T> 类型
     * @return 返回实例
     */
     public static <T> T getPerson(Class<T> clazz){
         try {
             return clazz.newInstance();
         } catch (InstantiationException e) {
             e.printStackTrace();
         } catch (IllegalAccessException e) {
             e.printStackTrace();
         }
         return null;
     }

    /**
     * 通过字段创建对象
     * @param type 类型支付
     * @return 实例对象
     */
     public static Person getPerson(String type){
         if (Objects.equals(type,"student")){
             return new Student();
         }else if(Objects.equals(type,"teacher")){
             return new Teacher();
         }else if (Objects.equals(type,null)){
             return null;
         }else{
             return null;
         }
     }

     /*end********简单工厂模式*******************/


}
